package com.base.common.taglib;

import java.io.IOException;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagHandler;
import com.sun.facelets.tag.jsf.ComponentConfig;

/**
 * units指机构，deps指部门，users指人员，roles指角色，各值之前用逗号分隔
 * 机构，部门，人员与角色之间是并集。其中一个条件成立，就不对其他条件进行判断。角色判断是交集，必须同时属于所有角色才能访问。
 * 
 * 
 */
public class IfAllGrantedTag extends TagHandler {
	private final TagAttribute units;
	private final TagAttribute deps;
	private final TagAttribute users;
	private final TagAttribute roles;
	private final TagAttribute resources;

	public IfAllGrantedTag(ComponentConfig componentConfig) {
		super(componentConfig);
		this.units = this.getAttribute("units");
		this.deps = this.getAttribute("deps");
		this.users = this.getAttribute("users");
		this.roles = this.getAttribute("roles");
		this.resources = this.getAttribute("resources");
	}

	@Override
	public void apply(FaceletContext faceletContext, UIComponent uiComponent)
			throws IOException, FacesException, FaceletException, ELException {
		if (this.roles != null) {
			String cRoles = null;
			ValueExpression ve = this.roles.getValueExpression(faceletContext,
					Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null)
					cRoles = obj.toString();
			}
			if (cRoles != null) {
				if (GrantedELTab.ifAllGranted(cRoles)) {
					this.nextHandler.apply(faceletContext, uiComponent);
					return;
				}
			}
		}
		if (this.resources != null) {
			String cRes = null;
			ValueExpression ve = this.resources.getValueExpression(
					faceletContext, Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null)
					cRes = obj.toString();
			}
			if (cRes != null) {
				if (GrantedELTab.ifResourceGranted(cRes)) {
					this.nextHandler.apply(faceletContext, uiComponent);
					return;
				}
			}
		}
		if (this.units != null) {
			String cUnits = null;
			ValueExpression ve = this.units.getValueExpression(faceletContext,
					Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null)
					cUnits = obj.toString();
			}
			if (cUnits != null) {
				if (GrantedELTab.unitGranted(cUnits)) {
					this.nextHandler.apply(faceletContext, uiComponent);
					return;
				}
			}
		}
		if (this.deps != null) {
			String cDepts = null;
			ValueExpression ve = this.deps.getValueExpression(faceletContext,
					Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null)
					cDepts = obj.toString();
			}
			if (cDepts != null) {
				if (GrantedELTab.depGranted(cDepts)) {
					this.nextHandler.apply(faceletContext, uiComponent);
					return;
				}
			}
		}
		if (this.users != null) {
			String cUsers = null;
			ValueExpression ve = this.users.getValueExpression(faceletContext,
					Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null)
					cUsers = obj.toString();
			}
			if (cUsers != null) {
				if (GrantedELTab.userGranted(cUsers)) {
					this.nextHandler.apply(faceletContext, uiComponent);
					return;
				}
			}
		}
	}
}
